package gofish.objects;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

/**
 * GameData is meant to simulate what Kylyn's code will do when it is deployed
 * on the server with mine. This class has the same static functions available
 * that hers will, though some changes are possible. This class simulates a
 * database by storing one Game object in a HashMap. My code uses this class's
 * functions to query and modify that Game object, just as they will in the
 * next stage of our project.
 *
 * @author Matt Lauber
 */
public class GameData {
	
	private static String deck = "";
	private static int eid = 0;

	private static int nextGid; // WILL BE REMOVED WHEN WE GET A GOOD WAY
	                         //   TO ALLOCATE GIDs
	
	private static HashMap<Integer, Game> gameMap = new HashMap<Integer, Game>();
	
	public static void incrementEid(int gid) {
		System.out.println("increment");
		++eid;
	}
	
	public static int getEventId(int gid) {
		return eid;
	}
	
	public static int startGame() {

		ArrayList<String> tempDeck = new ArrayList<String>();
		String[] num = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K"};
		String[] suits = {"H", "D", "C", "S"};
		for (int i = 0; i < num.length; ++i) {
			for (int j = 0; j < suits.length; ++j) {
				tempDeck.add(num[i] + suits[j]);
			}
		}

		Collections.shuffle(tempDeck);
		deck = tempDeck.get(0);

		for (int i = 1; i < tempDeck.size(); ++i) {
			deck += tempDeck.get(i);
		}
		
		gameMap.put(1, new Game(1));
		return 1;
	}
	
	public static String goFish(int gid) { // gid used in next version

		String card = deck.substring(0, 2);
		
		try {
			deck = deck.substring(2);
		}
		catch (Exception e) {
			deck = "";
		}
		
		return card;
	}
	
	public static boolean isDeckEmpty(int gid) { // gid used in next version
		
		return deck.isEmpty();
	}
	
	public static boolean isPlayerHandEmpty(int gid, String uid) {
		
		return gameMap.get(1).getPlayer(uid).getHand().equals("");
	}
	
	public static boolean isCardInOpponentHand(int gid, String uid, String card) {
		Game game = gameMap.get(1);
		Player player = game.getPlayer(uid);
		String hand = player.getHand();
		
		if (hand.contains( String.valueOf(card.charAt(0) )) ) {
			return true;
		}
		else {
			return false;
		}
	}
	
	public static boolean isCardInPlayerHand(int gid, String uid, String card) {
		Game game = gameMap.get(1);
		Player player = game.getPlayer(uid);
		String hand = player.getHand();
		
		if (hand.contains(card) ) {
			return true;
		}
		else {
			return false;
		}
	}
	
	public static void addCardToPlayerHand(int gid, String uid, String card) {
		Game game = gameMap.get(1);
		Player player = game.getPlayer(uid);
		player.setHand(player.getHand() + card);
	}
	
	public static void removeCardFromPlayerHand(int gid, String uid, String card) {
		Game game = gameMap.get(1);
		Player player = game.getPlayer(uid);
		String hand = player.getHand();
		
		String newHand = "";
		for (int i = 0; i < hand.length(); i += 2) {
			if (hand.charAt(i) == card.charAt(0)) {
				continue;
			}
			newHand += hand.substring(i, i + 2);
		}
		
		player.setHand(newHand);
	}
	
	public static void addCardToPlayerPairs(int gid, String uid, String card) {
		
		Game game = gameMap.get(1);
		Player player = game.getPlayer(uid);
		
		player.setPairs(player.getPairs() + String.valueOf(card.charAt(0)));
		System.out.println("Card " + card + " added");
	}

	
	public static int refresh(int gid, int eidParam) {
		
		return eid - eidParam;
	}
	
	public static Game getGameState(int gid) {
		
		return gameMap.get(1);
		
	}
	
	
	
	public static int addGame(Game game) {
		
		gameMap.put(1, game);
		return 1;
	}
	
	public static void endGame(int gid) {
		eid = -1;
		// remove game
	}
	
	public static int getNextGid() {
		return nextGid++;
	}
	
	public static void nextTurn(int gid) {
		Game game = gameMap.get(1);
		game.incrementTurn();
	}
	
	public static void joinGroup(int gid, String userName) {
		Game game = gameMap.get(1);
		Player p = new Player(userName);
		game.addPlayer(p);
		for (int i = 0; i < 7; ++i) {
		//for (int i = 0; i < 4; ++i) {
			String card = goFish(gid);
			if (GameData.isCardInOpponentHand(gid, userName, card)) {
				GameData.removeCardFromPlayerHand(gid, userName, card);
				GameData.addCardToPlayerPairs(gid, userName, card);
			}
			else {
				p.setHand(p.getHand() + card);
			}
		}
		//game.addPlayer(p);
	}
}
